package wnxy.wn44.shopwn.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wnxy.wn44.shopwn.entity.TShopcart;
import wnxy.wn44.shopwn.service.ShopService;

import java.math.BigDecimal;
import java.util.List;

@Api("商品控制器")
@Slf4j
@Controller
@RequestMapping("/shop")
public class ShopController {
    private ShopService shopService;

    @Autowired
    public void setShopService(ShopService shopService) {
        this.shopService = shopService;
    }

    @ResponseBody
    @RequestMapping("/listShop")
    public List<TShopcart> findProryListBypage(){
        List<TShopcart> list = shopService.findAll();
        return list;
    }

    @ResponseBody
    @RequestMapping("/addShop")
    public String addPoetry( @RequestBody TShopcart tShopcart){
        BigDecimal price = tShopcart.getPrice();
        Long num = tShopcart.getNum();
        Double subtotal=Double.parseDouble(String.valueOf(price))*num;
        tShopcart.setSubtotal(new BigDecimal(subtotal));
        tShopcart.setPimage("http://localhost/productimg/p3.webp");
        return shopService.addShop(tShopcart)?"ok":"no";
    }


    @ResponseBody
    @GetMapping("/removeById/{id}")
    public String removeById(@PathVariable("id") Integer id){
        return shopService.deletebyId(id)?"ok":"no";
    }


    @ResponseBody
    @RequestMapping("/updateShop")
    public String updatePoetry(@RequestBody TShopcart tShopcart){
        BigDecimal price = tShopcart.getPrice();
        Long num = tShopcart.getNum();
        Double subtotal=Double.parseDouble(String.valueOf(price))*num;
        tShopcart.setSubtotal(new BigDecimal(subtotal));
        return shopService.updateShop(tShopcart)?"ok":"no";
    }


}
